package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.dto.AdminOrders.AdminOrdersGetDetailDto;
import com.shop.entity.pojo.Orders;
import com.shop.entity.pojo.OrdersDetail;
import com.shop.admin.mapper.OrdersDetailMapper;
import com.shop.admin.mapper.OrdersMapper;
import com.shop.admin.service.OrdersDetailService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class OrdersDetailServiceImpl extends ServiceImpl<OrdersDetailMapper, OrdersDetail>
    implements OrdersDetailService{

    @Autowired
    private OrdersDetailMapper ordersDetailMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<List<OrdersDetail>> getOrdersDetail(AdminOrdersGetDetailDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Orders orders = ordersMapper.selectById(dto.getOrdersId());
        if (orders==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        String key=Message.Admin_key_Orders_Detail+dto.getOrdersId();
        List<OrdersDetail> check = (List<OrdersDetail>) redisTemplate.opsForValue().get(key);
        if (check!=null&&check.size()>Message.Check_Int_Null) {
            return Result.okResult(check);
        }
        List<OrdersDetail> detailList = ordersDetailMapper.getList(orders.getOrdersId());
        if (CollectionUtils.isEmpty(detailList) ||detailList.size()== Message.Check_Int_Null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        return Result.okResult(detailList);
    }
}




